package com.dh.jarvis.Util;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 日期处理工具类
 *
 * @author M.c 说明：对日期格式的格式化与转换操作等一系列操作
 */
@SuppressLint({"SimpleDateFormat", "DefaultLocale"})
public class DateUtil {

    public static enum TIME_ACCURACY {
        SECOND, MINUTE
    }

    public static String dateToString(String longdate) {

        return dateToString("yyyy-MM-dd", new Date(Long.parseLong(longdate + "000")));
    }

    public static String dateToStringS(String longdate) {

        return dateToString("yyyy-MM-dd  HH:mm:ss", new Date(Long.parseLong(longdate + "000")));
    }
    public static String dateToStringHHmm(String longdate) {

        return dateToString("HH:mm", new Date(Long.parseLong(longdate + "000")));
    }

    /**
     * 字符串转换成日期 yyyy-MM-dd HH:mm:ss
     *
     * @param strFormat 格式定义 如：yyyy-MM-dd HH:mm:ss
     * @param dateValue 日期对象
     * @return
     */
    public static Date stringToDate(String strFormat, String dateValue) {
        if (dateValue == null)
            return null;
        if (strFormat == null)
            strFormat = "yyyy-MM-dd HH:mm:ss";

        SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
        Date newDate = null;
        try {
            newDate = dateFormat.parse(dateValue);
        } catch (ParseException e) {
            newDate = null;
            e.printStackTrace();
        }
        return newDate;
    }

    /**
     * 日期转成字符串
     *
     * @param strFormat 格式定义 如：yyyy-MM-dd HH:mm:ss
     * @param date      日期字符串
     * @return
     */
    public static String dateToString(String strFormat, Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
        return dateFormat.format(date);
    }

    /**
     * 计算两个日期间隔天数
     *
     * @param begin
     * @param end
     * @return
     */
    public static int countDays(Date begin, Date end) {
        int days = 0;
        Calendar c_b = Calendar.getInstance();
        Calendar c_e = Calendar.getInstance();
        c_b.setTime(begin);
        c_e.setTime(end);
        while (c_b.before(c_e)) {
            days++;
            c_b.add(Calendar.DAY_OF_YEAR, 1);
        }
        return days;
    }

    /**
     * 计算距离当前日期间隔天数
     *
     * @return
     */
    public static int countDays(Date date) {
        int days = 0;
        Date now = new Date();
        Calendar c_b = Calendar.getInstance();
        Calendar c_e = Calendar.getInstance();
        c_b.setTime(now);
        c_e.setTime(date);
        if (c_b.before(c_e)) {
            while (c_b.before(c_e)) {
                days++;
                c_b.add(Calendar.DAY_OF_YEAR, 1);
            }
        } else {
            while (c_e.before(c_b)) {
                days++;
                c_e.add(Calendar.DAY_OF_YEAR, 1);
            }
        }

        return days;
    }

    /**
     * 获取当前日期，格式：yyyy-MM-dd
     *
     * @return
     */
    public static String getDateNow() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }

    /**
     * 获取当前时间，格式：HH:mm:ss
     *
     * @return
     */
    public static String getTimeNow() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(new Date());
    }

    /**
     * 获取星期几
     *
     * @return "一","二"等
     */
    public static String getWeekNow() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        switch (Integer.parseInt(mWay)) {
            default:
                break;
            case 1:
                mWay = "日";
                break;
            case 2:
                mWay = "一";
                break;
            case 3:
                mWay = "二";
                break;
            case 4:
                mWay = "三";
                break;
            case 5:
                mWay = "四";
                break;
            case 6:
                mWay = "五";
                break;
            case 7:
                mWay = "六";
                break;
        }

        return mWay;
    }

    /**
     * 获取N天后的日期
     * @param N 几天后
     * @return 2015-10-10-日
     */
    public static String getDayN(int N){
        String date = null,mWay = null;
        int year = 0;
        int month = 0;
        int week = 0;
        int day = 0;
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DAY_OF_MONTH, N);

        year = c.get(Calendar.YEAR);//几几年
        month = c.get(Calendar.MONTH)+1;//几月
        week = c.get(Calendar.DAY_OF_WEEK);//周几
        day = c.get(Calendar.DAY_OF_MONTH);//几号
        switch (week) {
            default:
                break;
            case 1:
                mWay = "日";
                break;
            case 2:
                mWay = "一";
                break;
            case 3:
                mWay = "二";
                break;
            case 4:
                mWay = "三";
                break;
            case 5:
                mWay = "四";
                break;
            case 6:
                mWay = "五";
                break;
            case 7:
                mWay = "六";
                break;
        }

        date = year+"-"+month+"-"+day+"-"+mWay;
        return date;
    }


    /**
     * 获取年份，格式：yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static int getYearByFomatDate(String date) {
        return Integer.parseInt(date.split("-")[0]);
    }

    /**
     * 获取月份，格式：yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static int getMonthByFomatDate(String date) {
        return Integer.parseInt(date.split("-")[1]);
    }

    /**
     * 获取日，格式：yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static int getDayByFomatDate(String date) {
        return Integer.parseInt(date.split("-")[2]);
    }

    /**
     * 是否是日期，格式：yyyy-MM-dd
     *
     * @return
     */
    public static boolean isDate(String strDate) {
        Pattern pattern = Pattern
                .compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
        Matcher m = pattern.matcher(strDate);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * date转化为long时间戳
     *
     * @param date 转换时间的date
     * @return
     */
    public static Long Date2Long(Date date) {

        return date.getTime();
    }

}
